package com.itheima.sfbx.insurance.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.sfbx.framework.commons.exception.ProjectException;
import com.itheima.sfbx.framework.commons.utils.BeanConv;
import com.itheima.sfbx.insurance.dto.CoefficentVO;
import com.itheima.sfbx.insurance.enums.CoefficentEnum;
import com.itheima.sfbx.insurance.mapper.CoefficentMapper;
import com.itheima.sfbx.insurance.pojo.CategoryCoefficent;
import com.itheima.sfbx.insurance.pojo.Coefficent;
import com.itheima.sfbx.insurance.pojo.InsuranceCoefficent;
import com.itheima.sfbx.insurance.service.ICategoryCoefficentService;
import com.itheima.sfbx.insurance.service.ICoefficentService;
import com.itheima.sfbx.insurance.service.IInsuranceCoefficentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
/**
 * @Description：系数项服务实现类
 */
@Slf4j
@Service
public class CoefficentServiceImpl extends ServiceImpl<CoefficentMapper, Coefficent> implements ICoefficentService {


    private LambdaQueryWrapper<Coefficent> queryWrapper(CoefficentVO coefficentVO){
        LambdaQueryWrapper<Coefficent> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StrUtil.isNotBlank(coefficentVO.getCoefficentKey()), Coefficent::getCoefficentKey, coefficentVO.getCoefficentKey())
                .like(StrUtil.isNotBlank(coefficentVO.getCoefficentKeyName()), Coefficent::getCoefficentKeyName, coefficentVO.getCoefficentKeyName())
                .eq(StrUtil.isNotBlank(coefficentVO.getDataState()), Coefficent::getDataState, coefficentVO.getDataState());
        return lqw;
    }
    @Override
    public Page<CoefficentVO> findPage(CoefficentVO coefficentVO, int pageNum, int pageSize) {
        if (ObjectUtil.isEmpty(coefficentVO)){
            log.error("系数项参数为空!,{}",coefficentVO);
            throw new ProjectException(CoefficentEnum.PAGE_FAIL);
        }
        LambdaQueryWrapper<Coefficent> lqw = queryWrapper(coefficentVO);
        Page<Coefficent> page = this.page(new Page<>(pageNum, pageSize), lqw);
        return BeanConv.toPage(page,CoefficentVO.class);
    }

    @Override
    public CoefficentVO findById(String coefficentId) {
        //TODO
        return null;
    }

    @Override
    @Transactional
    public CoefficentVO save(CoefficentVO coefficentVO) {
        Integer count = lambdaQuery()
                .eq(Coefficent::getCoefficentKey, coefficentVO.getCoefficentKey())
//                .eq(Coefficent::getDataState, coefficentVO.getDataState())
                .count();
        if (count>0){
            log.error("该系数项已存在:{}",coefficentVO.getCoefficentKey());
            throw new ProjectException(CoefficentEnum.SAVE_FAIL);
        }
        Coefficent bean = BeanConv.toBean(coefficentVO, Coefficent.class);
        this.save(bean);
        return BeanConv.toBean(bean,CoefficentVO.class);
    }

    @Autowired
    private ICategoryCoefficentService categoryCoefficentService;
    @Autowired
    private IInsuranceCoefficentService insuranceCoefficentService;
    @Override
    @Transactional
    public Boolean update(CoefficentVO coefficentVO) {
        Coefficent coefficent = this.getById(coefficentVO.getId());
        if (ObjectUtil.isEmpty(coefficent)){
            log.error("找不到该系数项,无法修改:{}",coefficentVO.getId());
            throw new ProjectException(CoefficentEnum.UPDATE_FAIL);
        }
        Integer count = lambdaQuery()
                .eq(Coefficent::getCoefficentKey, coefficentVO.getCoefficentKey())
                .ne(Coefficent::getId, coefficentVO.getId())
                .count();
        if (count>0){
            log.error("系数项key已存在,无法修改:{}",coefficentVO.getId());
            throw new ProjectException(CoefficentEnum.UPDATE_FAIL);
        }
        Integer count1 = categoryCoefficentService.lambdaQuery().eq(CategoryCoefficent::getCoefficentKey,coefficentVO.getCoefficentKey()).count();
        Integer count2 = insuranceCoefficentService.lambdaQuery().eq(InsuranceCoefficent::getCoefficentKey,coefficentVO.getCoefficentKey()).count();
        if (count1>0||count2>0){
            log.error("有关联,无法修改:{}",coefficentVO.getCoefficentKey());
            throw new ProjectException(CoefficentEnum.UPDATE_FAIL);
        }
        boolean flag = updateById(BeanConv.toBean(coefficentVO, Coefficent.class));
        return flag;
    }

    @Override
    @Transactional
    public Boolean delete(String[] checkedIds) {
        for (String checkedId : checkedIds) {
            Coefficent coefficent = this.getById(checkedId);
            if (ObjectUtil.isEmpty(coefficent)){
                log.error("找不到该系数项,无法删除:{}",checkedId);
                throw new ProjectException(CoefficentEnum.DEL_FAIL);
            }
            Integer count1 = categoryCoefficentService.lambdaQuery().eq(CategoryCoefficent::getCoefficentKey,coefficent.getCoefficentKey()).count();
            Integer count2 = insuranceCoefficentService.lambdaQuery().eq(InsuranceCoefficent::getCoefficentKey,coefficent.getCoefficentKey()).count();
            if (count1>0||count2>0){
                log.error("有关联,无法删除:{}",coefficent.getCoefficentKey());
                throw new ProjectException(CoefficentEnum.DEL_FAIL);
            }
            removeById(checkedId);
        }
        return true;
    }

    @Override
    public List<CoefficentVO> findList(CoefficentVO coefficentVO) {
        LambdaQueryWrapper<Coefficent> lqw = queryWrapper(coefficentVO);
        List<Coefficent> list = this.list(lqw);
        return BeanConv.toBeanList(list,CoefficentVO.class);
    }

    @Override
    public CoefficentVO findByCoefficentKey(String coefficentKey) {
        //TODO
        return null;
    }
}
